<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
      <el-form-item :label="$t('sysruledata.name')" prop="name">
        <el-input v-model="dataForm.name" :placeholder="$t('sysruledata.name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysruledata.value')" prop="value">
        <el-input v-model="dataForm.value" :placeholder="$t('sysruledata.value')"></el-input>
      </el-form-item>
      <el-form-item prop="ruleSort" :label="$t('sysruledata.ruleSort')">
        <el-select v-if="!dataForm.id" v-model="dataForm.ruleSort" :placeholder="$t('sysruledata.ruleSort')">
          <el-option v-for="ruleSort in ruleSortList" :key="ruleSort.value" :label="ruleSort.name" :value="ruleSort.value"></el-option>
        </el-select>
        <el-tag v-else>{{ $getDataItem(ruleSortList, dataForm.ruleSort, "value").name }}</el-tag>
      </el-form-item>
      <el-form-item :label="$t('sysruledata.sort')" prop="sort">
        <el-input-number v-model="dataForm.sort" :min="0" :max="10000"></el-input-number>
      </el-form-item>
      <el-form-item :label="$t('sysruledata.remark')" prop="remark">
        <el-input v-model="dataForm.remark" :placeholder="$t('sysruledata.remark')" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysruledata.ruleSql')" prop="ruleSql">
        <el-input v-model="dataForm.ruleSql" :placeholder="$t('sysruledata.ruleSqlTips')" type="textarea" :autosize="{ minRows: 2, maxRows: 4}"></el-input>
      </el-form-item>
      <el-form-item :label="$t('sysruledata.ruleArgs')" prop="ruleArgs">
        <el-input v-model="dataForm.ruleArgs" :placeholder="$t('sysruledata.ruleArgsTips')" type="textarea" :autosize="{ minRows: 1, maxRows: 4}"></el-input>
      </el-form-item>

    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import mixinViewModule from '@/mixins/view-module'
import aes from '@/utils/aes.js'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      visible: false,
      loading: false,
      dataForm: {
        id: '',
        name: '',
        value: '',
        ruleSort: '',
        sort: '',
        creator: '',
        createDate: '',
        remark: '',
        ruleSql: '',
        ruleArgs: ''
      }
    }
  },
  props: {
    ruleSortList: {
      type: Array,
      default () {
        return []
      }
    }
  },
  computed: {
    dataRule () {
      return {
        name: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        value: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        ruleSort: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ],
        sort: [
          {
            required: true,
            message: this.$t('validate.required'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.loading = true
      this.$http.get(`/sys/sysruledata/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs['dataForm'].validate(valid => {
          if (!valid) {
            return false
          }
          this.loading = true
          let params = {
            ...this.dataForm,
            ruleSql: aes.encrypt(this.dataForm.ruleSql, this.$store.state.passwordEncKey),
            ruleArgs: aes.encrypt(this.dataForm.ruleArgs, this.$store.state.passwordEncKey)
          }
          this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/sysruledata/', params).then(({ data: res }) => {
            this.loading = false
            if (res.code !== 0) {
              return
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }).catch(() => { })
        })
      },
      1000,
      { leading: true, trailing: false }
    )
  }
}
</script>
